<?php
namespace Admin\Controller;

class UserController extends AdminController{
	public function index(){
		$p = empty($_GET['p'])?'1':$_GET['p'];
		//echo "Admin user index";
		$model = M('student');
		$count = $model->count();
		$page  = new \Think\Page($count,'5');
		$show = $page->show();
		$data = $model->order('id desc')->page($p.',5')->select();
		$this->assign('title','学员信息管理系统');
		$this->assign('data',$data);
		$this->assign('show',$show);
		$this->display();
	}

	/**
	* delete删除方法
	* @param int $id 删除的id
	*/
	public function delete(){
		$id = I('get.id/d',0);
		//删除
		if(M('student')->delete($id) > 0 ){
			//$this->success('恭喜你删除成功！',U('index'));
			echo 1;
		} else {
			//$this->error('删除失败！');
			echo 0;
		}
	}
	//添加页面
	public function add(){
		$this->assign('title','学员信息添加');
		$this->display();
	}

	public function insert(){
		//判断是否POST提交的数据
		if(!IS_POST){
			$this->redirect('index');
			exit;
		}

		//对数据进行创建  筛选 针对你new数据表
		$model = M('student');

//		$data ['name '] = $_POST['name'];
//		$obj = new stdClass();
//		$obj->name =$_POST['name'];

		$model->create();

		if($model->add() > 0){
			$this->success('恭喜你，添加成功！',U('index'));
		}else{
			$this->error('恭喜你，添加成功！');
		}

	}

	//按指定id拿去数据渲染模版
	public function edit(){
		$id = I('get.id/d',0);
		$data = M('student')->find($id);
		if(!$data){
			$this->error('暂无数据');
			exit;
		}

		$this->assign('title','学员数据修改');
		$this->assign('data',$data);
		$this->display();
	}

	public function update(){
		//判断是否POST提交的数据
		if(!IS_POST){
			$this->redirect('index');
			exit;
		}
		//对数据进行创建  筛选 针对你new数据表
		$model = M('student');

		$model->create();

		if($model->save() > 0){
			$this->success('恭喜你，修改成功！',U('index'));
		}else{
			$this->error('恭喜你，修改失败！');
		}
	}

	public function dbList(){
		//$model = new \Think\Model('student');
		$model = M('student');
		//dump($model);
		$data = $model->order('id desc')->select();
		dump($data);
	}

	public function test(){
		$model = D('student');
		$data = $model->demo();
		$data = $model->select();
		dump($data);
	}
}